package com.question.controller.user;

import com.question.constant.RedisKeyConstant;
import com.question.pojo.dto.CommentDTO;
import com.question.pojo.result.Result;
import com.question.pojo.vo.CommentVO;
import com.question.service.CommentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/users/comments")
@Slf4j
@Api(tags = "评论相关接口")
public class CommentController {

    @Autowired
    private CommentService commentService;

    /**
     * 添加评论
     * @param commentDTO
     * @return
     */
    @PostMapping("/add")
    @ApiOperation("添加评论")
    @Caching(evict = {@CacheEvict(cacheNames = RedisKeyConstant.REDIS_USER_LISTCOMMENT_KEY, allEntries = true)
    })
    public Result add(@RequestBody CommentDTO commentDTO) {
        log.info("添加评论：{}", commentDTO);
        commentService.addComment(commentDTO);
        return Result.success();
    }

    /**
     * 根据题目查询评论
     * @param questionId
     * @return
     */
    @GetMapping("/get/{questionId}")
    @ApiOperation("根据题目查询评论")
    @Cacheable(cacheNames = RedisKeyConstant.REDIS_USER_LISTCOMMENT_KEY, key = "#questionId")
    public Result<List<CommentVO>> getCommentsByQuestionId(@PathVariable Integer questionId) {
        log.info("根据题目查询评论：{}", questionId);
        List<CommentVO> commentVOList = commentService.getCommentsByQuestionId(questionId);
        return Result.success(commentVOList);
    }

    /**
     * 删除评论
     * @param commentId
     * @return
     */
    @DeleteMapping("/delete/{commentId}")
    @ApiOperation("删除评论")
    @Caching(evict = {@CacheEvict(cacheNames = RedisKeyConstant.REDIS_USER_LISTCOMMENT_KEY, allEntries = true)
    })
    public Result deleteComment(@PathVariable Integer commentId) {
        log.info("删除评论：{}", commentId);
        commentService.deleteCommentById(commentId);
        return Result.success();
    }
}
